//
//  File.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/29.
//  Copyright © 2018 LKGood. All rights reserved.
//

import Foundation
import MediaPlayer

extension PlayCore {
  
  func registerMPRemoteCommand() {
    
    let remoteCommandCenter = MPRemoteCommandCenter.shared()
    
    // play & pause
    remoteCommandCenter.playCommand.isEnabled = true
    remoteCommandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      self.playPause()
      return MPRemoteCommandHandlerStatus.success
    }
    remoteCommandCenter.pauseCommand.isEnabled = true
    remoteCommandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      self.playPause()
      return MPRemoteCommandHandlerStatus.success
    }
    remoteCommandCenter.togglePlayPauseCommand.isEnabled = true
    remoteCommandCenter.togglePlayPauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      self.playPause()
      return MPRemoteCommandHandlerStatus.success
    }
    
    // stop
    remoteCommandCenter.stopCommand.isEnabled = true
    remoteCommandCenter.stopCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      loggingPrint(event.command.description)
      return MPRemoteCommandHandlerStatus.success
    }
    
    // prev
    remoteCommandCenter.previousTrackCommand.isEnabled = true
    remoteCommandCenter.previousTrackCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      if NowPlayingManager.shared.canPlayPrevAuidoInList {
        NowPlayingManager.shared.playPrevAudioInList()
      }
      return MPRemoteCommandHandlerStatus.success
    }

    // next
    remoteCommandCenter.nextTrackCommand.isEnabled = true
    remoteCommandCenter.nextTrackCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      if NowPlayingManager.shared.canPlayNextAuidoInList {
        NowPlayingManager.shared.playNextAudioInList()
      }
      return MPRemoteCommandHandlerStatus.success
    }
    
    /*
    // skip forward
    remoteCommandCenter.skipForwardCommand.isEnabled = true
    remoteCommandCenter.skipForwardCommand.preferredIntervals = [NSNumber(integerLiteral: 15)]
    remoteCommandCenter.skipForwardCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      self.skipForward15Seconds()
      return MPRemoteCommandHandlerStatus.success
    }

    // skip backward
    remoteCommandCenter.skipBackwardCommand.isEnabled = true
    remoteCommandCenter.skipBackwardCommand.preferredIntervals = [NSNumber(integerLiteral: 15)]
    remoteCommandCenter.skipBackwardCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      self.skipBackward15Seconds()
      return MPRemoteCommandHandlerStatus.success
    }
    */
    
    // change playback position
    remoteCommandCenter.changePlaybackPositionCommand.isEnabled = true
    remoteCommandCenter.changePlaybackPositionCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
      if let event = event as? MPChangePlaybackPositionCommandEvent {
        self.seek(toProgress: Float(event.positionTime) / Float(self.playerItem!.duration.seconds))
      }
      return MPRemoteCommandHandlerStatus.success
    }
  }
}
